package Presentacion.academico;

import global.Dialogos;
import global.Iconos;

public final class dlgPeriodo extends javax.swing.JDialog {
    private Iconos ico = new Iconos();

    public dlgPeriodo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        iconos();
        setTitle("RGISTRO DE PERIODO LECTIVO");
        setLocationRelativeTo(this);
        setResizable(false);
        iniciar();
        visiblidad(true);
    }
    
    private void iconos() {
        btnnuevo.setIcon(ico.getNuevo());
        btnguardar.setIcon(ico.getGuardar());
        btnsalir.setIcon(ico.getSalir());
        btnactualizar.setIcon(ico.getActualizar());
    }

    protected void iniciar() {
        txtperiodo.setText("");
        this.dtfechaini.setDate(global.Util.StringaDate(global.Util.getFechaActual()));
        this.dtfechafini.setDate(global.Util.StringaDate(global.Util.getFechaActual()));
        btngenerar.requestFocus();

    }

    public void visiblidad(boolean visibilidad) {
        if (visibilidad != true) {
            btnactualizar.setVisible(true);
            btnguardar.setVisible(false);
        } else {
            btnactualizar.setVisible(false);
            btnguardar.setVisible(true);
        }
    }

    private boolean Validacion() {
        if ("".equals(txtperiodo.getText())) {
            Dialogos.FaltaCampo("Periodo");
            txtperiodo.requestFocus();
            return false;
        } else if (this.dtfechafini.getDate().before(this.dtfechaini.getDate())) {
            global.Mensajes.mostrarMensaje("Advertencia!", "La fecha de fin de matriculacion debe ser mayor a la fecha de inicio", 2);
            return false;
        } else {
            return true;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar4 = new javax.swing.JToolBar();
        btnnuevo = new javax.swing.JButton();
        btnguardar = new javax.swing.JButton();
        btnactualizar = new javax.swing.JButton();
        btnsalir = new javax.swing.JButton();
        panelPeriodo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtperiodo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        dtfechaini = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        dtfechafini = new com.toedter.calendar.JDateChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        dtInicio = new com.toedter.calendar.JYearChooser();
        jLabel3 = new javax.swing.JLabel();
        dtfin = new com.toedter.calendar.JYearChooser();
        btngenerar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jToolBar4.setBorder(null);
        jToolBar4.setFloatable(false);
        jToolBar4.setBorderPainted(false);

        btnnuevo.setText("Nuevo");
        btnnuevo.setFocusable(false);
        btnnuevo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnnuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnnuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnuevoActionPerformed(evt);
            }
        });
        jToolBar4.add(btnnuevo);

        btnguardar.setText("Guardar");
        btnguardar.setFocusable(false);
        btnguardar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnguardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnguardar);

        btnactualizar.setText("Actualizar");
        btnactualizar.setFocusable(false);
        btnactualizar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnactualizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnactualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnactualizarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnactualizar);

        btnsalir.setText("Salir");
        btnsalir.setFocusable(false);
        btnsalir.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnsalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });
        jToolBar4.add(btnsalir);

        panelPeriodo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("PERIODO LECTIVO");

        txtperiodo.setEditable(false);
        txtperiodo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtperiodoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtperiodoKeyTyped(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("INICIO MATRICULACIÓN");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("FIN MATRICULACIÓN");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Generar Periodo"));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("INICIO");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("FIN");

        btngenerar.setText("Generar");
        btngenerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btngenerarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(dtInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(dtfin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(88, 88, 88)
                .addComponent(btngenerar)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel3)
                        .addComponent(dtfin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btngenerar)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(dtInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panelPeriodoLayout = new javax.swing.GroupLayout(panelPeriodo);
        panelPeriodo.setLayout(panelPeriodoLayout);
        panelPeriodoLayout.setHorizontalGroup(
            panelPeriodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPeriodoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPeriodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panelPeriodoLayout.createSequentialGroup()
                        .addGroup(panelPeriodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelPeriodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dtfechaini, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                            .addComponent(dtfechafini, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 115, Short.MAX_VALUE))
                    .addGroup(panelPeriodoLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(45, 45, 45)
                        .addComponent(txtperiodo)))
                .addContainerGap())
        );
        panelPeriodoLayout.setVerticalGroup(
            panelPeriodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPeriodoLayout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(panelPeriodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtperiodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(panelPeriodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(dtfechaini, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(panelPeriodoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(dtfechafini, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jToolBar4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelPeriodo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar4, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelPeriodo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnnuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnuevoActionPerformed
        iniciar();
        visiblidad(true);

    }//GEN-LAST:event_btnnuevoActionPerformed

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        if (Validacion() == true) {
            frmConsultaPeriodo.objPeriodo.setPeriodo(txtperiodo.getText());
            frmConsultaPeriodo.objPeriodo.setFechainicio(global.Util.DateaString(this.dtfechaini.getDate()));
            frmConsultaPeriodo.objPeriodo.setFechafinal(global.Util.DateaString(this.dtfechafini.getDate()));
            if (frmConsultaPeriodo.objPeriodo.guardarPeriodoLectivo() == true) {
                frmConsultaPeriodo.generartabla();
                dispose();
                iniciar();
            }
        }
    }//GEN-LAST:event_btnguardarActionPerformed

    private void btnactualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnactualizarActionPerformed
        if (Validacion() == true) {
            if (frmConsultaPeriodo.objPeriodo.ExistenciaPeriodo(txtperiodo.getText()) == true) {
                global.Mensajes.mostrarMensaje("Advertencia!", "El periodo ya existe", 2);
            } else {
                frmConsultaPeriodo.objPeriodo.setPeriodo(txtperiodo.getText());
                frmConsultaPeriodo.objPeriodo.setFechainicio(global.Util.DateaString(this.dtfechaini.getDate()));
                frmConsultaPeriodo.objPeriodo.setFechafinal(global.Util.DateaString(this.dtfechafini.getDate()));
                if (frmConsultaPeriodo.objPeriodo.actualizarPeriodoLectivo() == true) {
                    frmConsultaPeriodo.generartabla();
                    dispose();
                    iniciar();
                }
            }
        }
    }//GEN-LAST:event_btnactualizarActionPerformed

    private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
        dispose();
        iniciar();

    }//GEN-LAST:event_btnsalirActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        iniciar();
    }//GEN-LAST:event_formWindowClosed

    private void btngenerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btngenerarActionPerformed
        if (dtfin.getYear() > dtInicio.getYear()) {
            if (dtfin.getYear() - dtInicio.getYear() == 1) {
                txtperiodo.setText("PERIODO LECTIVO " + dtInicio.getYear() + " - " + dtfin.getYear());
            } else {
                global.Mensajes.mostrarMensaje("Advertencia!", "El Fin del periodo solo puede ser mayor un año al Inicio", 2);
            }
        } else {
            global.Mensajes.mostrarMensaje("Advertencia!", "El Fin del periodo no puede ser menor o igual al Inicio", 2);
        }
    }//GEN-LAST:event_btngenerarActionPerformed

    private void txtperiodoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtperiodoKeyTyped
        global.Validaciones.numerosLetras(evt, txtperiodo);
    }//GEN-LAST:event_txtperiodoKeyTyped

    private void txtperiodoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtperiodoKeyReleased
        global.Validaciones.convertirMayuscula(txtperiodo);
    }//GEN-LAST:event_txtperiodoKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgPeriodo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgPeriodo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgPeriodo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgPeriodo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                dlgPeriodo dialog = new dlgPeriodo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnactualizar;
    private javax.swing.JButton btngenerar;
    private javax.swing.JButton btnguardar;
    private javax.swing.JButton btnnuevo;
    private javax.swing.JButton btnsalir;
    private com.toedter.calendar.JYearChooser dtInicio;
    public com.toedter.calendar.JDateChooser dtfechafini;
    public com.toedter.calendar.JDateChooser dtfechaini;
    private com.toedter.calendar.JYearChooser dtfin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JPanel panelPeriodo;
    public javax.swing.JTextField txtperiodo;
    // End of variables declaration//GEN-END:variables
}
